<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增政策新闻')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>

</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-policynews-add">

            <div class="row">
                <div class="col-sm-6">

                    <div class="form-group">
                        <label class="col-sm-3 control-label"><span style="color: red; ">*</span>标题：</label>
                        <div class="col-sm-8">
                            <input name="title" id="title" class="form-control" type="text">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label"><span style="color: red; ">*</span>简介：</label>
                        <div class="col-sm-8">
                            <textarea name="introduction" class="form-control"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label"><span style="color: red; ">*</span>类型：</label>
                        <div class="col-sm-8">

                            <select name="newsType" id="newsType" class="form-control m-b"
                                    th:with="type=${@dict.getType('plicy_news_type')}">
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>

                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-3 control-label"><span style="color: red; ">*</span>发布时间：</label>
                        <div class="col-sm-8">
                            <div class="input-group date">
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                <input name="issueTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            </div>
                        </div>
                    </div>

                </div>

                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">主图上传：</label>
                        <div class="col-sm-8">

                            <input id="upload" name="file" type="file">
                            <input id="mainImg" name="mainImg"  type="hidden">
                        </div>
                    </div>
                </div>

            </div>

            <div class="row">

                <div class="form-group" id="twdiv">
                    <div class="col-sm-12">
                        <label class="col-sm-2 control-label"><span style="color: red; ">*</span>图文详情：</label>
                        <div class="col-sm-10">
                            <!-- 富文本编辑框-->
                            <div class="col-sm-10">

                                <div class="summernote"></div>
                                <input type="hidden" name="content" id="content" value="value">

                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-6 col-sm-10">
                        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i
                                class="fa fa-check"></i>保存政策新闻信息
                        </button>
                    </div>
                </div>


            </div>

        </form>
    </div>
    <th:block th:include="include :: footer"/>
    <th:block th:include="include :: datetimepicker-js"/>

    <th:block th:include="include :: select2-js"/>
    <th:block th:include="include :: summernote-js"/>
    <th:block th:include="include :: bootstrap-fileinput-js"/>

    <script th:inline="javascript">
        var ctx = [[${#request.getContextPath()}]];
        var prefix = ctx + "/agriculture/policynews"
        $("#form-policynews-add").validate({
            focusCleanup: true
        });

        $("input[name='issueTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(document).ready(function () {
            $("#upload").fileinput({
                theme: 'explorer-fas',
                uploadUrl: ctx + "/oss/upload",
                enctype: 'multipart/form-data',
                overwriteInitial: false,
                initialPreviewAsData: true,
                //图片最大7M
                maxFileSize: 1024 * 7,
                allowedFileTypes: ['image'],    // allow only images
            }).on('fileuploaded', function (event, data) {
                data = data.response;
                if (data.code !== 0) {
                    console.error(data.msg);
                } else {
                    $("#mainImg").val(data.data);
                }
            }).on('fileuploaderror', function (event, data, msg) {
                console.log('File Upload Error', 'ID: ' + data.fileId + ', Thumb ID: ' + data.previewId);
            }).on('filebatchuploadcomplete', function (event, preview, config, tags, extraData) {
                console.log('File Batch Uploaded', preview, config, tags, extraData);
            });
        });


        $('.summernote').summernote({
            placeholder: '请输入公告内容',
            height: 192,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "/oss/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (result) {
                    if (result.code == 0) {
                        $(obj).summernote('editor.insertImage', result.data, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function (error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }


        function submitHandler() {
            if ($.validate.form()) {
                var sHTML = $('.summernote').summernote('code');
                $("#content").val(sHTML);

                var obj = $('#form-policynews-add').serialize();
                $.operate.save(prefix + "/add", obj);
            }
        }

    </script>
</body>
</html>
